package com.wbdgj.ui.store.cafe;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.base.BaseFragment;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnNoResponseListener;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.branch.NetfeeRoamingActivity;
import com.wbdgj.ui.home.BranchCouponActivity;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.ui.mine.InformaticaNewActivity;
import com.wbdgj.ui.store.MemberPointsActivity;
import com.wbdgj.ui.store.MissionCenterActivity;
import com.wbdgj.ui.store.PointsDetailsActivity;
import com.wbdgj.ui.store.PointsMallActivity;
import com.wbdgj.ui.store.PointsMallDetailsActivity;
import com.wbdgj.ui.store.StoreCofeActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.dialog.ConfirmDialog;
import com.wbdgj.views.AllRoundImageView;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import retrofit2.Call;

public class StoreCofeFragment04 extends BaseFragment implements PtrHandler {

    private Context context;

    @BindView(R.id.pfl_root)
    PtrClassicFrameLayout pfl_root;

    @BindView(R.id.roundImg)
    AllRoundImageView roundImg;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.wfTxt)
    TextView wfTxt;

    @BindView(R.id.yeTxt)
    TextView yeTxt;

    @BindView(R.id.card)
    TextView card;
    @BindView(R.id.sjztTxt)
    TextView sjztTxt;
    @BindView(R.id.yhqTxt)
    TextView yhqTxt;
    @BindView(R.id.jfTxt)
    TextView jfTxt;

    private String BRANCH_ID;
    private String BRANCH_NAME;

    @BindView(R.id.myImg)
    ImageView myImg;
    @BindView(R.id.ycxjImg)
    ImageView ycxjImg;
    private boolean isRoam = true;

    private void isopenRoam() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().isopenRoam(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), BRANCH_ID);
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    myImg.setImageResource(R.mipmap.store_my_icon);
                    isRoam = true;
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    myImg.setImageResource(R.mipmap.store_my_icon2);
                    isRoam = false;
                }
            }
        });
    }

    @Override
    protected int getLayout() {
        return R.layout.ui_story_cofe_fragment_04;
    }

    @Override
    protected void onInit() {
        context = getActivity();
        pfl_root.setEnabledNextPtrAtOnce(true);
        pfl_root.setLastUpdateTimeRelateObject(this);
        pfl_root.setPtrHandler(this);
        pfl_root.setKeepHeaderWhenRefresh(true);
        pfl_root.disableWhenHorizontalMove(true);
        BRANCH_ID = BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID);
        BRANCH_NAME = BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_NAME);
        getMbUserInfo();
        appCouponlistmy();
        getMyIntegralInfoByApp();
        isopenRoam();
        rocordgroupa();
        isonline();
    }

    private void getMyIntegralInfoByApp() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getMyIntegralInfoByApp(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID),BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if(linkedTreeMap.containsKey("data")){
                        LinkedTreeMap dateLink = (LinkedTreeMap) linkedTreeMap.get("data");
                        if(dateLink.containsKey("AvailableIntegral")){
                            jfTxt.setText(decimalFormat.format(dateLink.get("AvailableIntegral")));
                        }
                    }

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

    private void appCouponlistmy() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().appCouponlistmy("1",BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), "10", "1", BRANCH_ID);
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    LinkedTreeMap page = (LinkedTreeMap) linkedTreeMap.get("page");
                    yhqTxt.setText(decimalFormat.format(page.get("totalCount")));

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    Intent intent = new Intent(context, LoginActivity.class);
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }


    //网吧会员信息
    private void rocordgroupa() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().memberinfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), BRANCH_ID);
        call.enqueue(new OnNoResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    LinkedTreeMap linkedTreeMapMember = (LinkedTreeMap) linkedTreeMap.get("member");
                    wfTxt.setText(linkedTreeMapMember.get("remain")+"");
                }
            }
        });
    }

    private void getMbUserInfo() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getMbUserInfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if(linkedTreeMap.containsKey("NAME")){
                        name.setText(""+linkedTreeMap.get("NAME"));
                    }else{
                        name.setText("游客");
                    }

                    if (!(linkedTreeMap.get("IMG")+"").equals("null")){
                        Glide.with(context).load(HttpAdapter.HEAD_URL+linkedTreeMap.get("IMG")).placeholder(R.mipmap.mine_photo).error(R.mipmap.mine_photo).into(roundImg);
                    }else{
                        Glide.with(context).load("").placeholder(R.mipmap.mine_photo).error(R.mipmap.mine_photo).into(roundImg);
                    }
                    userinfo(linkedTreeMap.get("ID_CARD")+"");
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    private void userinfo(String ID_CARD) {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().userinfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID), ID_CARD, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnNoResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if(linkedTreeMap.containsKey("code")){
                        if(decimalFormat.format(linkedTreeMap.get("code")).equals("200")){
                            LinkedTreeMap dataLink = (LinkedTreeMap) linkedTreeMap.get("data");
                            card.setText(dataLink.get("level")+"");
                            if(dataLink.containsKey("totalBalance")){
                                yeTxt.setText("￥"+ decimalFormat.format(dataLink.get("totalBalance"))+"");
                            }

                        }
                    }
//                    card.setText(linkedTreeMap.get("lv_name")+"");
//                    account_balance.setText("￥"+linkedTreeMap.get("account_balance")+"");
                }
            }
        });
    }


    @Override
    public void fetchData() {

    }

    @OnClick({R.id.yeLay, R.id.wfLay, R.id.jfLay, R.id.yhqLay,R.id.ddLay, R.id.cwfLay, R.id.myLay, R.id.dzLay, R.id.hdLay, R.id.ycxjLay, R.id.jfscLay, R.id.wdddLay, R.id.wddpLay, R.id.myClick})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.myClick:
                startActivity(new Intent(context, InformaticaNewActivity.class));
                break;
            case R.id.yeLay:
                ToastUtils.toastShort("暂未开放");
                break;
            case R.id.wfLay:
                ToastUtils.toastShort("暂未开放");
                break;
            case R.id.jfLay:
                launch(MemberPointsActivity.class);
                break;
            case R.id.yhqLay:
                startActivity(new Intent(context, BranchCouponActivity.class).putExtra("branch_id", BRANCH_ID));
                break;
            case R.id.ddLay:
                ToastUtils.toastShort("暂未开放");
                break;
            case R.id.cwfLay:
                ToastUtils.toastShort("暂未开放");
                break;
            case R.id.myLay:
                if (isRoam){
                    startActivityForResult(new Intent(context, NetfeeRoamingActivity.class).putExtra("id", BRANCH_ID)
                            .putExtra("name", BRANCH_NAME),1);
                }else{
                    final ConfirmDialog confirmDialog = new ConfirmDialog(context, "当前门店没有开通此功能！", 8);
                    confirmDialog.show();
                    confirmDialog.setCanceledOnTouchOutside(false);
                    confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                        @Override
                        public void doConfirm() {
                            confirmDialog.dismiss();
                        }

                        @Override
                        public void doCancel() {
                            confirmDialog.dismiss();
                        }
                    });
                }

                break;
            case R.id.dzLay:
                ToastUtils.toastShort("暂未开放");
                break;
            case R.id.hdLay:
                StoreCofeActivity.choiceFragment(2);
                break;
            case R.id.ycxjLay:
                final ConfirmDialog confirmDialogbalance = new ConfirmDialog(context, "确定下机吗？", 1);
                confirmDialogbalance.show();
                confirmDialogbalance.setCanceledOnTouchOutside(false);
                confirmDialogbalance.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                    @Override
                    public void doConfirm() {
                        confirmDialogbalance.dismiss();
                        balance();
                    }

                    @Override
                    public void doCancel() {
                        confirmDialogbalance.dismiss();
                    }
                });
                break;
            case R.id.jfscLay:
                launch(PointsMallActivity.class);
                break;
            case R.id.wdddLay:
                ToastUtils.toastShort("暂未开放");
                break;
            case R.id.wddpLay:
                ToastUtils.toastShort("暂未开放");
                break;

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1){
            if (resultCode == 1){
                rocordgroupa();
            }
        }
    }

    private void balance() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().balance(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), BRANCH_ID);
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    final ConfirmDialog confirmDialog = new ConfirmDialog(context, userModelBaseResultModel.getResultMsg(), 8);
                    confirmDialog.show();
                    confirmDialog.setCanceledOnTouchOutside(false);
                    confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                        @Override
                        public void doConfirm() {
                            confirmDialog.dismiss();
                        }

                        @Override
                        public void doCancel() {
                            confirmDialog.dismiss();
                        }
                    });
                    rocordgroupa();
                    isonline();
                }else if(userModelBaseResultModel.getResultCode().equals("0404")){
                    final ConfirmDialog confirmDialog = new ConfirmDialog(context, userModelBaseResultModel.getResultMsg(), 8);
                    confirmDialog.show();
                    confirmDialog.setCanceledOnTouchOutside(false);
                    confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                        @Override
                        public void doConfirm() {
                            confirmDialog.dismiss();
                        }

                        @Override
                        public void doCancel() {
                            confirmDialog.dismiss();
                        }
                    });
                }else{
                    final ConfirmDialog confirmDialog = new ConfirmDialog(context, "自助下机失败！", 8);
                    confirmDialog.show();
                    confirmDialog.setCanceledOnTouchOutside(false);
                    confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                        @Override
                        public void doConfirm() {
                            confirmDialog.dismiss();
                        }

                        @Override
                        public void doCancel() {
                            confirmDialog.dismiss();
                        }
                    });
                }
            }
        });
    }

    private void isonline() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().isonline(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), BRANCH_ID);
        call.enqueue(new OnNoResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if (decimalFormat.format(linkedTreeMap.get("code")).equals("200")){
                        LinkedTreeMap linkedTreeMapData = (LinkedTreeMap) linkedTreeMap.get("data");

                        if ((linkedTreeMapData.get("computer")+"").equals("")){
                            sjztTxt.setText("已激活");

                        }else{
                            sjztTxt.setText("上机中,机器号："+linkedTreeMapData.get("computer")+",已消费："+linkedTreeMapData.get("remain_fee"));
                        }
                    }else{
                        sjztTxt.setText("未上机");

                    }
                }
            }
        });
    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        getMbUserInfo();
        appCouponlistmy();
        getMyIntegralInfoByApp();
        isopenRoam();
        rocordgroupa();
        isonline();
        pfl_root.refreshComplete();
    }
}
